<?php
/**
 *  院校数据的编辑
 *  by HehongChen
 *  2014.10.23
 *
 */
require_once(dirname(__FILE__)."/config.php");
require_once(DEDEINC.'/enums.func.php');
if(empty($dopost)) $dopost = '';


else if($dopost!='save')
{
    $college_id = intval($college_id);

    // 读取collegeinfo、collegeinfo_detail数据库
    // 每插入一条collegeinfo的数据，必定会插入一条collegeinfo_detail的数据，此处用and
    $inquery = "SELECT * FROM `#@__collegeinfo` col,`#@__collegeinfo_detail` coldetail WHERE col.collegeId=$college_id AND coldetail.collegeId=$college_id";
    $collegeinfo_totle = $dsql->GetOne($inquery);
    if(!is_array($collegeinfo_totle))
    {
        ShowMsg("读取数据库出错!","-1");
        exit();
    }

    // 读取collegeinfo_recruit、collegeinfo_graduate、collegeinfo_professor、collegeinfo_project、collegeinfo_news数据表
    $inquery = "SELECT * FROM `#@__collegeinfo_recruit` WHERE collegeId=$college_id";
    $dsql->SetQuery($inquery);
    $dsql->Execute();
    $collegeinfo_recruit = array();
    while($row = $dsql->GetArray()){
        $collegeinfo_recruit[] = $row;
    }


    $inquery = "SELECT * FROM `#@__collegeinfo_graduate` WHERE collegeId=$college_id";
    $dsql->SetQuery($inquery);
    $dsql->Execute();
    $collegeinfo_graduate = array();
    while($row = $dsql->GetArray()){
        $collegeinfo_graduate[] = $row;
    }

    $inquery = "SELECT * FROM `#@__collegeinfo_professor` WHERE collegeId=$college_id";
    $dsql->SetQuery($inquery);
    $dsql->Execute();
    $collegeinfo_professor = array();
    while($row = $dsql->GetArray()){
        $collegeinfo_professor[] = $row;
    }

    $inquery = "SELECT * FROM `#@__collegeinfo_project` WHERE collegeId=$college_id";
    $dsql->SetQuery($inquery);
    $dsql->Execute();
    $collegeinfo_project = array();
    while($row = $dsql->GetArray()){
        $collegeinfo_project[] = $row;
    }

    $inquery = "SELECT * FROM `#@__collegeinfo_news` WHERE collegeId=$college_id";
    $dsql->SetQuery($inquery);
    $dsql->Execute();
    $collegeinfo_news = array();
    while($row = $dsql->GetArray()){
        $collegeinfo_news[] = $row;
    }
    
    include DedeInclude("templets/collegeinfo_edit.htm");
    exit();
}

else if($dopost=='save')
{
    // 取得院校id
    $college_id = intval($college_id);

    // 下面对数据进行处理后更新到相应的数据表中
    // 处理dede_collegeinfo数据表的数据
    // 院校性质处理
    if ($level != null) {
        foreach ($level as $key => $value) {
            $levels .= $value.','; 
        }
        $levels = rtrim($levels,',');
    }
    // 项目类型的处理
    if ($project_type != null) {
        foreach ($project_type as $key => $value) {
            $project_types .= $value.','; 
        }
        $project_types = rtrim($project_types,',');
    }

    // 校徽的处理
    if(is_uploaded_file($logo_new))
    {
       // 先删除以前的校徽
       @unlink("../..".$logo);
       $logo_url = UploadFile($logo_new,$logo_new_name,"collegeinfo/logo");
    }else{
       $logo_url = $logo;
    }
    // 更新collegeinfo数据表
    $inQuery = "UPDATE `#@__collegeinfo` SET area='$area',address='$address',lowestFee='$lowest_fee',highestFee='$highest_fee',feeRange='$fee_range',name='$name',englishName='$english_name',recommend='$recommend',logo='$logo_url',level='$levels',interAttest='$inter_attest',projectType='$project_types',scoreType='$score_type',projectSummary='$project_summary',foundTime='$found_time' WHERE collegeId=$college_id;";
    if(!$dsql->ExecuteNoneQuery($inQuery))
    {
        ShowMsg("更新数据库__collegeinfo 时出错，请检查原因！", "-1");
        exit();
    }

    
    // 处理collegeinfo_deatail数据表的数据
    // 处理院校照片
    if(is_uploaded_file($picture_new))
    {
       // 先删除以前的图片
       @unlink("../..".$picture);   
       $picture_url = UploadFile($picture_new,$picture_new_name,"collegeinfo/collegeinfo_pictures");
    }
    else{
        $picture_url = $picture;
    }
    // 处理资讯页栏目照片
    if(is_uploaded_file($news_picture_new))
    {
       // 先删除以前的图片
       @unlink("../..".$news_picture);
       $news_picture_url = UploadFile($news_picture_new,$news_picture_new_name,"collegeinfo/collegeinfo_news_pictures");
    }
    else{
        $news_picture_url = $news_picture;
    }
    // 处理分数线
    $score_line_log = nl2br($score_line_log);
    // 更新collegeinfo_deatail数据表
    $inQuery = "UPDATE `#@__collegeinfo_detail` SET collegeId='$college_id',feeIntroduction='$fee_introduction',picture='$picture_url',newsPicture='$news_picture_url',detailSummary='$detail_summary',internetSite='$internet_site',eduTime='$edu_time',regulator='$regulator',recruitNum='$recruit_num',scholarship='$scholarship',scoreLineLog='$score_line_log' WHERE collegeId=$college_id;";
    if(!$dsql->ExecuteNoneQuery($inQuery))
    {
        ShowMsg("更新数据库__collegeinfo_detail 时出错，请检查原因！", "-1");
        exit();
    }

    // 处理collegeinfo_recruit数据表的数据
    // 先删除collegeinfo_recruit数据表原来的数据
    $dsql->ExecuteNoneQuery("DELETE FROM `#@__collegeinfo_recruit` WHERE collegeId='$college_id'");
    // 再循环将三个年份数据存入数据表
    for ($i=0; $i < 3; $i++) { 
        $deal_year = intval($recruit_year[$i]);
        $deal_apply_num = $recruit_apply_num[$i];
        $deal_exam_num = $recruit_exam_num[$i];
        $deal_recruit_num = $recruit_recruit_num[$i];
        // 年份非空就存进数据表
        if ($deal_year!='') {
            $deal_percent = (intval($deal_recruit_num)*1.0)/intval($deal_exam_num);
            $inQuery = "INSERT INTO `#@__collegeinfo_recruit`(collegeId,year,applyNum,examNum,recruitNum,recruitProportion)VALUES('$college_id','$deal_year','$deal_apply_num','$deal_exam_num','$deal_apply_num','$deal_percent');";
            if(!$dsql->ExecuteNoneQuery($inQuery))
            {
                $gerr = $dsql->GetError();
                ShowMsg("把数据保存到数据库主表 `#@__collegeinfo_recruit` 时出错。".str_replace('"','',$gerr),"javascript:;");
                exit();
            }
        }
    }

    // 处理collegeinfo_news数据表的数据
    // 先删除collegeinfo_news数据表原来的数据
    $dsql->ExecuteNoneQuery("DELETE FROM `#@__collegeinfo_news` WHERE collegeId='$college_id'");
    // 再循环将资讯信息数据存入数据表
    for ($i=0; $i < 7; $i++) { 
        $deal_news_title = $news_title[$i];
        $deal_news_link = $news_link[$i];
        $deal_news_addtime = strtotime($news_addtime[$i]);
        // 新闻标题非空就存进数据表
        if ($deal_news_title!='') {
            $inQuery = "INSERT INTO `#@__collegeinfo_news`(collegeId,title,link,addTime)VALUES('$college_id','$deal_news_title','$deal_news_link','$deal_news_addtime');";
            if(!$dsql->ExecuteNoneQuery($inQuery))
            {
                $gerr = $dsql->GetError();
                ShowMsg("把数据保存到数据库主表 `#@__collegeinfo_news` 时出错。".str_replace('"','',$gerr),"javascript:;");
                exit();
            }
        }
    }

    // 处理collegeinfo_professor数据表的数据
    // 先删除collegeinfo_professor数据表中原来的数据
    $dsql->ExecuteNoneQuery("DELETE FROM `#@__collegeinfo_professor` WHERE collegeId='$college_id'");
    // 获取professor数据数组中最高元素的key值
    $hignestkey_professor = intval($txtTRLastIndex_professor)-1;
    // 再循环读取各个教授的信息存入数据表
    for ($i=0; $i <= $hignestkey_professor; $i++) { 
        if ($professor_name[$i]!='') {
            $pro_name = $professor_name[$i];
            $picture_url = '';
            // 处理教授照片
            if(is_uploaded_file($professor_picture_new[$i]))
            {
                // 删除掉以前的图片
                @unlink("../..".$professor_picture[$i]);
                $pro_picture = $professor_picture_new[$i];
                $picture_url = UploadFile($pro_picture,$pro_picture_name,"collegeinfo/professor_pictures");
            }
            else{
                $picture_url = $professor_picture[$i];
            }
            // 存进数据表
            $inQuery = "INSERT INTO `#@__collegeinfo_professor`(collegeId,name,picture)VALUES('$college_id','$pro_name','$picture_url');";
            if(!$dsql->ExecuteNoneQuery($inQuery))
            {
                $gerr = $dsql->GetError();
                ShowMsg("把数据保存到数据库主表 `#@__collegeinfo_professor` 时出错。".str_replace('"','',$gerr),"javascript:;");
                exit();
            }
        }
    }
    // 处理collegeinfo_graduate数据表的数据
    // 先删除collegeinfo_graduate数据表中原来的数据
    $dsql->ExecuteNoneQuery("DELETE FROM `#@__collegeinfo_graduate` WHERE collegeId='$college_id'");
    // 获取graduate数据数组中最高元素的key值
    $hignestkey_graduate = intval($txtTRLastIndex_graduate)-1;
    // 再循环读取各个教授的信息存入数据表
    for ($i=0; $i <= $hignestkey_graduate; $i++) { 
        if ($graduate_name[$i]!='') {
            $pro_name = $graduate_name[$i];
            $picture_url = '';
            // 处理教授照片
            if(is_uploaded_file($graduate_picture_new[$i]))
            {
                // 删除掉以前的图片
                @unlink("../..".$graduate_picture[$i]);
                $pro_picture = $graduate_picture_new[$i];
                $picture_url = UploadFile($pro_picture,$pro_picture_name,"collegeinfo/graduate_pictures");
            }
            else{
                $picture_url = $graduate_picture[$i];
            }
            // 存进数据表
            $inQuery = "INSERT INTO `#@__collegeinfo_graduate`(collegeId,name,picture)VALUES('$college_id','$pro_name','$picture_url');";
            if(!$dsql->ExecuteNoneQuery($inQuery))
            {
                $gerr = $dsql->GetError();
                ShowMsg("把数据保存到数据库主表 `#@__collegeinfo_graduate` 时出错。".str_replace('"','',$gerr),"javascript:;");
                exit();
            }
        }
    }

    // 处理collegeinfo_project数据表的数据
    // 先删除collegeinfo_project数据表中原来的数据
    $dsql->ExecuteNoneQuery("DELETE FROM `#@__collegeinfo_project` WHERE collegeId='$college_id'");
    // 获取project数据数组中最高元素的key值
    $hignestkey_project = intval($txtTRLastIndex_project)-1;
    // 再循环读取各个教授的信息存入数据表
    for ($i=0; $i <= $hignestkey_project; $i++) { 
        if ($project_title[$i]!='') {
            $proje_title = $project_title[$i];
            $proje_content = $project_content[$i];
            
            // 存进数据表
            $inQuery = "INSERT INTO `#@__collegeinfo_project`(collegeId,title,content)VALUES('$college_id','$proje_title','$proje_content');";
            if(!$dsql->ExecuteNoneQuery($inQuery))
            {
                $gerr = $dsql->GetError();
                ShowMsg("把数据保存到数据库主表 `#@__collegeinfo_project` 时出错。".str_replace('"','',$gerr),"javascript:;");
                exit();
            }
        }
    }

    ShowMsg("更新院校信息成功！","collegeinfo_list.php");
}


/**
 * 递归创建文件夹
 * @param [type] $path [目录路径]
 */
function MakeDir($path){
    if(is_dir($path)){
        return true;
    }
    return is_dir(dirname($path))||MakeDir(dirname($path))?mkdir($path):false;
}

/**
 * 上传图片文件的函数
 * @param [type] $image_temp [图片文件的缓存temp格式]
 * @param [type] $image_name [本地图片文件名]
 * @param [type] $path       [存储在upload中的目录]
 */
function UploadFile($image_temp,$image_name,$path){
    global $cfg_medias_dir,$cfg_basedir;
    $names = split("\.", $image_name);
    $shortname = ".".$names[count($names)-1];
    if(!preg_match("#(jpg|gif|png)$#", $shortname))
    {
        $shortname = '.gif';
    }
    $filename = MyDate("ymdHis", time()).mt_rand(1000,9999).$shortname;
    $imgurl = $cfg_medias_dir."/".$path;
    if(!is_dir($cfg_basedir.$imgurl))
    {
        MakeDir($cfg_basedir.$imgurl);
        CloseFtp();
    }
    $imgurl = $imgurl."/".$filename;
    move_uploaded_file($image_temp,$cfg_basedir.$imgurl) or die("复制文件到:".$cfg_basedir.$imgurl."失败");
    @unlink($image_temp);
    return $imgurl;
}